package development.app.mxh;

import iplayparticle.view.iplayparticleResources;
import state.DrawListener;
import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.Sprite;
import state.TouchScreenListener;
import state.component.Event;
import state.component.EventListener;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import development.Constants;
import development.DrawUtils;
import development.Global;
import development.app.FrmMangXH;
import development.socket.SocketClientUtil;

public class FrmSendMessage extends GameForm implements EventListener, TouchScreenListener {

  private ImageText text8;
  private final int DEFAULT_TEXT_X = 5;
  private int dy;

  private TextField txtTitle;
  private TextField txtContent;
  private Sprite createButtonSend;
  private GameImage buttonImage;

  private int chooseIndex;
  private String recipient;
  private Hashtable _parameters;

  public static GameForm backForm;
  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmSendMessage();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    this._parameters = parameters;
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    dy = DrawUtils.getTopBarHeight();
    chooseIndex = 0;
    recipient = "";
    txtTitle = new TextField(_manager, TextField.CONSTRAINT_ANY, null);
    txtTitle.setSize(GameConstants.SCREEN_WIDTH - (text8.stringWidth("Nội dung") + 10), text8.getHeight());
    txtTitle.setPosition(DEFAULT_TEXT_X, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 5);
    txtTitle.setVisible(true);
    txtTitle.setAnchor(GameConstants.TOP_LEFT_ANCHOR);
    txtTitle.setFocused(true);

    txtContent = new TextField(_manager, TextField.CONSTRAINT_ANY, null);
    txtContent.setSize(GameConstants.SCREEN_WIDTH - DEFAULT_TEXT_X * 2, text8.getHeight() * 2);
    txtContent.setPosition(DEFAULT_TEXT_X, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 5);
    txtContent.setVisible(true);
    txtContent.setAnchor(GameConstants.TOP_LEFT_ANCHOR);

    buttonImage = new GameImage(iplayparticleResources.image_button, null, null);
    createButtonSend = new Sprite(null, _manager, 0, 0, Constants.TOP_LEFT_ANCHOR);
    createButtonSend.setImage(buttonImage);
    createButtonSend.setTouchScreenListener(this);
    createButtonSend.setVisible(true);
    createButtonSend.setAnchor(GameConstants.TOP_LEFT_ANCHOR);
    createButtonSend.setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        text8.drawString2(g, "Gửi", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
      }
    });
    recipient = (String) parameters.get("User");
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawAppBackground(g);
    text8.setBold(true);
    int textY = dy + 20;
    text8.drawString2(g, "Gửi", GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR);
    text8.drawString2(g, recipient, GameColor.WHITE_CODE, text8.stringWidth("Nội dung") + 5, textY, GameConstants.TOP_LEFT_ANCHOR);
    textY = textY + 25;
    text8.drawString2(g, "Tiêu đề", GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR);
    txtTitle.setPosition(text8.stringWidth("Nội dung") + 5, textY);
    textY = textY + 25;
    text8.drawString2(g, "Nội dung", GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR);
    textY = textY + 25;
    txtContent.setPosition(DEFAULT_TEXT_X, textY);
    textY = textY + 40;
    createButtonSend.setPosition(txtContent.getRealX(), textY);
    text8.setBold(false);

    DrawUtils.drawTopBar(g);
    text8.drawString2(g, "Gửi tin nhắn", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 0, GameConstants.TOP_HCENTER_ANCHOR);
    drawMenu(g);
  }

  private void drawMenu(Graphics g) {
    DrawUtils.getBottomBar(_manager).setLabels("Trở về", "", "");
    if (txtTitle.isFocused() || txtContent.isFocused()) {
      DrawUtils.getBottomBar(_manager).setRightLabel("Xóa");
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.SOFT_RIGHT:
      if (txtTitle.isFocused()) {
        txtTitle.deleteLastChar();
      }
      if (txtContent.isFocused()) {
        txtContent.deleteLastChar();
      }
      break;
    case Key.SOFT_LEFT:
      if (backForm != null) {
        GameGlobal.nextState(backForm, _parameters);
      } else {
        GameGlobal.nextState(FrmMangXH.instance(), _parameters);
      }
      break;
    case Key.DOWN:
      if (chooseIndex < 2) {
        chooseIndex++;
      }
      if (chooseIndex == 0) {
        txtTitle.setFocused(true);
        txtContent.setFocused(false);
      }
      if (chooseIndex == 1) {
        txtContent.setFocused(true);
        txtTitle.setFocused(false);
      }
      if (chooseIndex == 2) {
        txtContent.setFocused(false);
        txtTitle.setFocused(false);
        createButtonSend.setDrawListener(new DrawListener() {
          public void paint(Sprite source, Graphics g) {
            text8.drawString2(g, "Gửi", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
          }
        });
      } else {
        createButtonSend.setDrawListener(new DrawListener() {
          public void paint(Sprite source, Graphics g) {
            text8.drawString2(g, "Gửi", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
          }
        });
      }
      break;
    case Key.UP:
      if (chooseIndex > 0) {
        chooseIndex--;
      }
      if (chooseIndex == 0) {
        txtTitle.setFocused(true);
        txtContent.setFocused(false);
      }
      if (chooseIndex == 1) {
        txtContent.setFocused(true);
        txtTitle.setFocused(false);
      }
      if (chooseIndex == 2) {
        txtContent.setFocused(false);
        txtTitle.setFocused(false);
        createButtonSend.setDrawListener(new DrawListener() {
          public void paint(Sprite source, Graphics g) {
            text8.drawString2(g, "Gửi", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
          }
        });
      } else {
        createButtonSend.setDrawListener(new DrawListener() {
          public void paint(Sprite source, Graphics g) {
            text8.drawString2(g, "Gửi", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
          }
        });
      }
      break;
    case Key.FIRE:
      if (txtTitle.isFocused()) {
        txtTitle.pointerReleased(txtTitle, txtTitle.getRealX(), txtTitle.getRealY());
      }
      if (txtContent.isFocused()) {
        txtContent.pointerReleased(txtContent, txtContent.getRealX(), txtContent.getRealY());
      }
      if (chooseIndex == 2) {
        String title = txtTitle.getText();
        if (title == null || title.equals("")) {
          title = "Không tiêu đề";
        }
        String content = txtContent.getText();
        if (content == null || content.equals("")) {
          Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn chưa viết nội dung");
          return;
        }
        SocketClientUtil.sendMessageToInbox(Global.currentUser.getName(), recipient, title, content);
        Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
      }
      break;
    default:
      if (txtTitle.isFocused()) {
        txtTitle.keyReleased1(keyCode);
      }
      if (txtContent.isFocused()) {
        txtContent.keyReleased1(keyCode);
      }
      break;
    }
  }

  public void pointerDragged(Sprite source, int x, int y) {
  }

  public void pointerPressed(Sprite source, int x, int y) {
  }

  public void pointerReleased(Sprite source, int x, int y) {
  }

  public void onActionPerform(Event event) {
  }

  public void destroy() {
    text8 = null;
    dy = DrawUtils.getTopBarHeight();
    txtTitle = null;
    txtContent = null;
    createButtonSend = null;
    buttonImage = null;
    chooseIndex = 0;
    backForm = null;
  }
}
